<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/05 19:58:31 $)
// $RCSfile: impex.knowledgebase.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Return the Total Pages for this Plugin
*/
function getPluginTotalPages3()
{
	return 4;
}

/**
* Run the specific page for plugin
*/
function runPluginPage3($page)
{
	global $impexDB, $_IMPEXHASH;

	// Import Categories
	if ($page == 1)
	{
		$categorylist = array();

		// First fetch old categories
		$impexDB->query("SELECT * FROM `tt_faq_category` ORDER BY `id` ASC;");
		while ($impexDB->nextRecord())
		{
			$categorylist[$impexDB->Record["id"]] = $impexDB->Record;
		}

		// Now insert new categories
		$_orderindex = 1;
		foreach ($categorylist as $key=>$val)
		{
			// Parent is set to 0 for now
			$_kbcategoryid = _impexInsertKBCategory(0, $val["name"], 0, $_orderindex);
			$_IMPEXHASH["kbcatpointer"][$key] = $_kbcategoryid;

			$_orderindex++;

			displayStatus("Imported KB Category: ".htmlspecialchars($val["name"]));
		}

		// Ok. Now that all categories are imported, we need to create a tree based on old data
		foreach ($categorylist as $key=>$val)
		{
			$oldcategoryid = $key;
			$newkbcategoryid = $_IMPEXHASH["kbcatpointer"][$key];

			if ($val["loc"] == "root")
			{
				$_parentid = 0;
			} else {
				$_parentid = intval($val["loc"]);
			}
			$newparentcategoryid = $_IMPEXHASH["kbcatpointer"][$_parentid];

			if ($newkbcategoryid == "" || ($newparentcategoryid == "" && $val["parentid"] != "0"))
			{
				displayStatus("<font color='red'>WARNING:</font> KB Category Import Failed. Invalid Pointer. $key ($newkbcategoryid - $newparentcategoryid): ".$val["parentid"]);
			} else {
				_impexUpdateKBCategoryParent($newkbcategoryid, $newparentcategoryid);

				displayStatus("Updated Tree for KB Category: ".htmlspecialchars($val["name"]));
			}

		}


	// Import Articles
	} else if ($page == 2) {
		$articlelist = array();

		// Fetch old questions
		$impexDB->query("SELECT * FROM `tt_faq_articles` ORDER BY `id` ASC;");
		while ($impexDB->nextRecord())
		{
			$articlelist[$impexDB->Record["id"]] = $impexDB->Record;
		}

		// Now insert the new kb articles
		$_orderindex = 1;
		foreach ($articlelist as $key=>$val)
		{
			$_kbarticleid = _impexInsertKBArticle($val["question"], $val["content"], $val["numview"], $_orderindex, returnSupportTrioTimestamp($val["date"]), 0, 0);

			$_IMPEXHASH["kbarticlepointer"][$key] = $_kbarticleid;

			displayStatus("Imported KB Article: ".htmlspecialchars($val["question"]));

			$_orderindex++;
		}


	// Insert Article <> Category Links
	} else if ($page == 3) {
		$articlelist = array();

		// Fetch old questions
		$impexDB->query("SELECT `id`, `cat` FROM `tt_faq_articles` ORDER BY `id` ASC;");
		while ($impexDB->nextRecord())
		{
			$articlelist[$impexDB->Record["id"]] = $impexDB->Record["cat"];
		}

		// Insert the Links
		foreach ($articlelist as $key=>$val)
		{
			$newkbcategoryid = $_IMPEXHASH["kbcatpointer"][$val];
			$newkbarticleid = $_IMPEXHASH["kbarticlepointer"][$key];

			if ($newkbcategoryid == "" || $newkbarticleid == "")
			{
				displayStatus("<font color='red'>WARNING:</font> Unable to Import Article Link. Invalid KB Category ID: $val");
			} else {
				_impexInsertKBArticleLink($newkbcategoryid, $newkbarticleid);

				displayStatus("Imported Article Link for Category (ID: ". intval($newkbcategoryid) .") and Article (ID: ". intval($newkbarticleid) .")");
			}
		}



	// Import all Comments
	} else if ($page == 4) {
		$commentlist = array();

		// Fetch old comments
		$impexDB->query("SELECT * FROM `tt_faq_comment` ORDER BY `id` ASC;");
		while ($impexDB->nextRecord())
		{
			$commentlist[$impexDB->Record["id"]] = $impexDB->Record;
		}

		foreach ($commentlist as $key=>$val)
		{
			$newkbarticleid = $_IMPEXHASH["kbarticlepointer"][$val["art"]];

			if ($newkbarticleid == "")
			{
				displayStatus("<font color='red'>WARNING:</font> Unable to Import Comment, Found a bogus KB Article: ".$val["art"]);
			} else {
				_impexInsertComment($newkbarticleid, COMMENT_KNOWLEDGEBASE, $val["name"], $val["email"], $val["comments"], 1, returnSupportTrioTimestamp($val["sdate"]));
			}

		}

		unset($_IMPEXHASH["kbarticlepointer"]);
		unset($_IMPEXHASH["kbcatpointer"]);
	}
	
}

?>